<template>
  <div class="goods">
    <mbx />
    <el-card>
      <!-- 搜索框 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="query.query"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="add()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="7">
          <el-button type="primary" @click="addition()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="600%">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.upd_time | dateFormate }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope.row.goods_id)"
            ></el-button>
            <el-button
              icon="el-icon-delete"
              type="danger"
              size="mini"
              @click="del(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 组件弹窗 -->
    <Dialog :forms="forms" :form="form" :options="options" @add='add'></Dialog>
  </div>
</template>

<script>
import mbx from "@/components/mbx.vue";
import { goodsuser, cateList, delList, idList } from "@/utils/Api(goods)/Goods";
import Dialog from "@/components/DialogGoods.vue";
export default {
  components: { mbx, Dialog },
  data() {
    return {
      tableData: [],
      query: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      forms: {
        title: "",
        type: "",
        flag: false,
      },
      form: {
        goods_name: "",
        goods_cat: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        goods_introduce: "",
        pics: [],
      },
      options: [],
    };
  },
  methods: {
    async add() {
      let { data } = await goodsuser(this.query);
      this.tableData = data.goods;
      this.total = data.total;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.pagesize = val;
      this.add();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.query.pagenum = val;
      this.add();
    },
    addition() {
      this.forms = {
        title: "添加商品",
        type: "add",
        flag: true,
      };
      this.form = {
        goods_name: "",
        goods_cat: "",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        goods_introduce: "",
        pics: [],
        id:''
      };
    },
    //获取级联选择器的数据
    async addrights() {
      let res = await cateList({ type: 3 });
      this.options = res.data;
    },
    //删除
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let data = await delList(id);
          if (data.meta.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          } else {
            this.$message.error(data.meta.msg);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑
    async edit(id) {
      this.forms = {
        title: "编辑商品",
        type: "edit",
        flag: true,
      };
      let { data } = await idList(id);
       this.form = {
        goods_name: data.goods_name,
        goods_price: data.goods_price,
        goods_number: data.goods_number,
        goods_weight: data.goods_weight,
        id:id
      };
      console.log(this.form);
    },
  },
  mounted() {
    this.add();
    this.addrights();
  },
};
</script>

<style lang="scss" scope>
.el-table {
  font-size: 13px;
}
</style>
